$(document).ready(function(){
	var om=true;
	var cu=0;
	$('#body').xheditor(true);	
	
	$("#ajaxStatus").ajaxStart(function(){
					$(this).fadeIn(500);
					$(this).html("<img src='../image/loading.gif' alt='Loading...' />");
				}).ajaxStop(function() {
					$(this).fadeOut(500);
				})
	
	$("#btnPost").click(function(){
		$.ajax({
			type: "POST",
			url: "addPost.do",
			dataType: "json",
			data: {
				"title":$("#title").val(),
				"body":$("#body").val(),
				"tags":$("#tags").val(),
				"category":$("input[name=categorys]:checked").val()
			},
			success:function(json){
				// alert("OK!");
				// $('#colattr').after("<tr id='"+json.msg.id+"' onclick='lineclick(this);'><td>"+json.msg.id+"</td><td>"+json.msg.from+"</td><td>"+json.msg.addDate+"</td><td>"+json.msg.mail+"</td><td>"+json.msg.site+"</td><td>"+json.msg.cont+"</td><td><a onclick='removerecordbyid("+json.msg.id+");'>ɾ��</a></td></tr>");
				// lineclick($("#"+json.msg.id));
			},
			error:function(){
				alert("NG!");
			}
		});
	});
	
	
	$("#showallmsgbtn").click(function(){
		$.ajax({
			type: "POST",
			url:"listAllRecord.action",
			dataType:"json",
			data:{},
			success:function(json){
				var strs="<table border=\"1\"><tr id='colattr'><td>key-id</td><td>�ǳ�</td><td>����ʱ��</td><td width='120'>����</td><td width='120'>����</td><td width='250'>����</td><td>����</td></tr>";
				for(i=0;i<json.records.length;i++){
					str="<tr id='"+json.records[i].id+"' onclick='lineclick(this);'><td>"+json.records[i].id+"</td><td>"+json.records[i].from+"</td><td>"+json.records[i].addDate+"</td><td>"+json.records[i].mail+"</td><td>"+json.records[i].site+"</td><td>"+json.records[i].cont+"</td><td><a onclick='removerecordbyid("+json.records[i].id+");'>ɾ��</a></td></tr>"
					strs=strs+str
				}
				strs=strs+"</table>"
				$("#showallrecord").html(strs);
			},
			error:function(){
				alert("error");
			}
		});
		
	});
});


	function lineclick(line) {
		$('#showallrecord table tr').each(function() {
					$(this).css("background-color", "#ffffff");
				});
		$(line).css("background-color", "#9FF");
	}
	
	function removerecordbyid(recordid){ 
   		$("#showallrecord table tr").each(
  		function(){
		  var seq=parseInt($(this).children("td").html());
		  var thisrecord = this;
		  if(seq==recordid)
		  	if(confirm("OK?")){
		  		$.ajax({
					type: "POST",
					url:"removeRecordById.action",
					dataType:"json",
					data:{"msg.id":recordid},
					success:function(json){
						if(json.status==4){
							alert("YYY");
						}else{
							$(thisrecord).remove();
						}
					},
					error:function(){
						alert("del error");
					}
				});
		  	}
		});
	}